package top.tangyh.lamp.care.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.annotations.Param;
import top.tangyh.basic.base.mapper.SuperMapper;
import top.tangyh.lamp.care.entity.BHl7Event;
import org.springframework.stereotype.Repository;
import top.tangyh.lamp.common.constant.EventEnum;

import java.util.Collection;
import java.util.List;

/**
 * <p>
 * Mapper 接口
 * Hl7病人事件表
 * </p>
 *
 * @author zqb
 * @date 2024-07-15 14:36:35
 * @create [2024-07-15 14:36:35] [zqb] [代码生成器生成]
 */
@Repository
public interface BHl7EventMapper extends SuperMapper<BHl7Event> {

    List<BHl7Event> selectEventListOfTopNewTime(List<String> hospitalNumbers, Integer eventType);

    default BHl7Event selectOneByEventTypeHospitalNumber(Integer eventType, String hospitalNumber) {
        LambdaQueryWrapper<BHl7Event> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BHl7Event::getEventType, eventType)
                .eq(BHl7Event::getHospitalNumber, hospitalNumber)
                .orderByDesc(BHl7Event::getEventTime)
                .last("limit 1");
        return this.selectOne(queryWrapper);
    }

    List<BHl7Event>  selectByEventTypeHospitalNumbers(@Param("eventType") Integer eventType, @Param("hospitalNumbers") Collection<String> hospitalNumbers);

    /**
     * 统计时间段内出院的人数
     *
     * @param startTime
     * @param endTime
     * @param deptCode
     * @return
     */
    default int countCy(String startTime, String endTime, String deptCode) {
        LambdaQueryWrapper<BHl7Event> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BHl7Event::getDeptIdent, deptCode)
                .eq(BHl7Event::getEventType, EventEnum.CY.getEventType())
                .ge(BHl7Event::getEventTime, startTime)
                .lt(BHl7Event::getEventTime, endTime);
        return Math.toIntExact(this.selectCount(queryWrapper));
    }

}


